
<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml" lang="zh_CN">
  <head>
    <meta charset="utf-8" />
    <title>pickletools --- pickle 开发者工具集 &#8212; Python 3.7.8 文档</title>
    <link rel="stylesheet" href="../_static/pydoctheme.css" type="text/css" />
    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
    
    <script type="text/javascript" id="documentation_options" data-url_root="../" src="../_static/documentation_options.js"></script>
    <script type="text/javascript" src="../_static/jquery.js"></script>
    <script type="text/javascript" src="../_static/underscore.js"></script>
    <script type="text/javascript" src="../_static/doctools.js"></script>
    <script type="text/javascript" src="../_static/language_data.js"></script>
    <script type="text/javascript" src="../_static/translations.js"></script>
    
    <script type="text/javascript" src="../_static/sidebar.js"></script>
    
    <link rel="search" type="application/opensearchdescription+xml"
          title="在 Python 3.7.8 文档 中搜索"
          href="../_static/opensearch.xml"/>
    <link rel="author" title="关于这些文档" href="../about.html" />
    <link rel="index" title="索引" href="../genindex.html" />
    <link rel="search" title="搜索" href="../search.html" />
    <link rel="copyright" title="版权所有" href="../copyright.html" />
    <link rel="next" title="杂项服务" href="misc.html" />
    <link rel="prev" title="dis --- Python 字节码反汇编器" href="dis.html" />
    <link rel="shortcut icon" type="image/png" href="../_static/py.png" />
    <link rel="canonical" href="https://docs.python.org/3/library/pickletools.html" />
    
    <script type="text/javascript" src="../_static/copybutton.js"></script>
    
    
    
    
    <style>
      @media only screen {
        table.full-width-table {
            width: 100%;
        }
      }
    </style>
 

  </head><body>
  
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>导航</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="总目录"
             accesskey="I">索引</a></li>
        <li class="right" >
          <a href="../py-modindex.html" title="Python 模块索引"
             >模块</a> |</li>
        <li class="right" >
          <a href="misc.html" title="杂项服务"
             accesskey="N">下一页</a> |</li>
        <li class="right" >
          <a href="dis.html" title="dis --- Python 字节码反汇编器"
             accesskey="P">上一页</a> |</li>
        <li><img src="../_static/py.png" alt=""
                 style="vertical-align: middle; margin-top: -1px"/></li>
        <li><a href="https://www.python.org/">Python</a> &#187;</li>
        <li>
          <a href="../index.html">3.7.8 Documentation</a> &#187;
        </li>

          <li class="nav-item nav-item-1"><a href="index.html" >Python 标准库</a> &#187;</li>
          <li class="nav-item nav-item-2"><a href="language.html" accesskey="U">Python 语言服务</a> &#187;</li>
    <li class="right">
        

    <div class="inline-search" style="display: none" role="search">
        <form class="inline-search" action="../search.html" method="get">
          <input placeholder="快速搜索" type="text" name="q" />
          <input type="submit" value="转向" />
          <input type="hidden" name="check_keywords" value="yes" />
          <input type="hidden" name="area" value="default" />
        </form>
    </div>
    <script type="text/javascript">$('.inline-search').show(0);</script>
         |
    </li>

      </ul>
    </div>    

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body" role="main">
            
  <div class="section" id="module-pickletools">
<span id="pickletools-tools-for-pickle-developers"></span><h1><a class="reference internal" href="#module-pickletools" title="pickletools: Contains extensive comments about the pickle protocols and pickle-machine opcodes, as well as some useful functions."><code class="xref py py-mod docutils literal notranslate"><span class="pre">pickletools</span></code></a> --- pickle 开发者工具集<a class="headerlink" href="#module-pickletools" title="永久链接至标题">¶</a></h1>
<p><strong>源代码:</strong> <a class="reference external" href="https://github.com/python/cpython/tree/3.7/Lib/pickletools.py">Lib/pickletools.py</a></p>
<hr class="docutils" />
<p>此模块包含与 <a class="reference internal" href="pickle.html#module-pickle" title="pickle: Convert Python objects to streams of bytes and back."><code class="xref py py-mod docutils literal notranslate"><span class="pre">pickle</span></code></a> 模块内部细节有关的多个常量，一些关于具体实现的详细注释，以及一些能够分析封存数据的有用函数。 此模块的内容对需要操作 <a class="reference internal" href="pickle.html#module-pickle" title="pickle: Convert Python objects to streams of bytes and back."><code class="xref py py-mod docutils literal notranslate"><span class="pre">pickle</span></code></a> 的 Python 核心开发者来说很有用处；<a class="reference internal" href="pickle.html#module-pickle" title="pickle: Convert Python objects to streams of bytes and back."><code class="xref py py-mod docutils literal notranslate"><span class="pre">pickle</span></code></a> 的一般用户则可能会感觉 <a class="reference internal" href="#module-pickletools" title="pickletools: Contains extensive comments about the pickle protocols and pickle-machine opcodes, as well as some useful functions."><code class="xref py py-mod docutils literal notranslate"><span class="pre">pickletools</span></code></a> 模块与他们无关。</p>
<div class="section" id="command-line-usage">
<h2>命令行语法<a class="headerlink" href="#command-line-usage" title="永久链接至标题">¶</a></h2>
<div class="versionadded">
<p><span class="versionmodified added">3.2 新版功能.</span></p>
</div>
<p>当从命令行发起调用时，<code class="docutils literal notranslate"><span class="pre">python</span> <span class="pre">-m</span> <span class="pre">pickletools</span></code> 将对一个或更多 pickle 文件的内容进行拆解。 请注意如果你查看 pickle 中保存的 Python 对象而非 pickle 格式的细节，你可能需要改用 <code class="docutils literal notranslate"><span class="pre">-m</span> <span class="pre">pickle</span></code>。 但是，当你想检查的 pickle 文件来自某个不受信任的源时，<code class="docutils literal notranslate"><span class="pre">-m</span> <span class="pre">pickletools</span></code> 是更安全的选择，因为它不会执行 pickle 字节码。</p>
<p>例如，对于一个封存在文件 <code class="docutils literal notranslate"><span class="pre">x.pickle</span></code> 中的元组 <code class="docutils literal notranslate"><span class="pre">(1,</span> <span class="pre">2)</span></code>:</p>
<div class="highlight-shell-session notranslate"><div class="highlight"><pre><span></span><span class="gp">$</span> python -m pickle x.pickle
<span class="gp gp-VirtualEnv">(1, 2)</span>

<span class="gp">$</span> python -m pickletools x.pickle
<span class="go">    0: \x80 PROTO      3</span>
<span class="go">    2: K    BININT1    1</span>
<span class="go">    4: K    BININT1    2</span>
<span class="go">    6: \x86 TUPLE2</span>
<span class="go">    7: q    BINPUT     0</span>
<span class="go">    9: .    STOP</span>
<span class="go">highest protocol among opcodes = 2</span>
</pre></div>
</div>
<div class="section" id="command-line-options">
<h3>命令行选项<a class="headerlink" href="#command-line-options" title="永久链接至标题">¶</a></h3>
<dl class="cmdoption">
<dt id="cmdoption-pickletools-a">
<code class="sig-name descname">-a</code><code class="sig-prename descclassname"></code><code class="sig-prename descclassname">, </code><code class="sig-name descname">--annotate</code><code class="sig-prename descclassname"></code><a class="headerlink" href="#cmdoption-pickletools-a" title="永久链接至目标">¶</a></dt>
<dd><p>使用简短的操作码描述来标注每一行。</p>
</dd></dl>

<dl class="cmdoption">
<dt id="cmdoption-pickletools-o">
<code class="sig-name descname">-o</code><code class="sig-prename descclassname"></code><code class="sig-prename descclassname">, </code><code class="sig-name descname">--output</code><code class="sig-prename descclassname">=&lt;file&gt;</code><a class="headerlink" href="#cmdoption-pickletools-o" title="永久链接至目标">¶</a></dt>
<dd><p>输出应当写入到的文件名称。</p>
</dd></dl>

<dl class="cmdoption">
<dt id="cmdoption-pickletools-l">
<code class="sig-name descname">-l</code><code class="sig-prename descclassname"></code><code class="sig-prename descclassname">, </code><code class="sig-name descname">--indentlevel</code><code class="sig-prename descclassname">=&lt;num&gt;</code><a class="headerlink" href="#cmdoption-pickletools-l" title="永久链接至目标">¶</a></dt>
<dd><p>一个新的 MARK 层级所需缩进的空格数。</p>
</dd></dl>

<dl class="cmdoption">
<dt id="cmdoption-pickletools-m">
<code class="sig-name descname">-m</code><code class="sig-prename descclassname"></code><code class="sig-prename descclassname">, </code><code class="sig-name descname">--memo</code><code class="sig-prename descclassname"></code><a class="headerlink" href="#cmdoption-pickletools-m" title="永久链接至目标">¶</a></dt>
<dd><p>当拆解多个对象时，保留各个拆解的备忘记录。</p>
</dd></dl>

<dl class="cmdoption">
<dt id="cmdoption-pickletools-p">
<code class="sig-name descname">-p</code><code class="sig-prename descclassname"></code><code class="sig-prename descclassname">, </code><code class="sig-name descname">--preamble</code><code class="sig-prename descclassname">=&lt;preamble&gt;</code><a class="headerlink" href="#cmdoption-pickletools-p" title="永久链接至目标">¶</a></dt>
<dd><p>当指定一个以上的 pickle 文件时，在每次反汇编之前打印给定的前言。</p>
</dd></dl>

</div>
</div>
<div class="section" id="programmatic-interface">
<h2>编程接口<a class="headerlink" href="#programmatic-interface" title="永久链接至标题">¶</a></h2>
<dl class="function">
<dt id="pickletools.dis">
<code class="sig-prename descclassname">pickletools.</code><code class="sig-name descname">dis</code><span class="sig-paren">(</span><em class="sig-param">pickle</em>, <em class="sig-param">out=None</em>, <em class="sig-param">memo=None</em>, <em class="sig-param">indentlevel=4</em>, <em class="sig-param">annotate=0</em><span class="sig-paren">)</span><a class="headerlink" href="#pickletools.dis" title="永久链接至目标">¶</a></dt>
<dd><p>将 pickle 的符号化反汇编数据输出到文件类对象 <em>out</em>，默认为 <code class="docutils literal notranslate"><span class="pre">sys.stdout</span></code>。 <em>pickle</em> 可以是一个字符串或一个文件类对象。 <em>memo</em> 可以是一个将被用作 pickle 的备忘记录的 Python 字典；它可被用来对由同一封存器创建的多个封存对象执行反汇编。 由 <code class="docutils literal notranslate"><span class="pre">MARK</span></code> 操作码指明的每个连续级别将会缩进 <em>indentlevel</em> 个空格。 如果为 <em>annotate</em> 指定了一个非零值，则输出中的每个操作码将以一个简短描述来标注。 <em>annotate</em> 的值会被用作标注所应开始的列的提示。</p>
<div class="versionadded">
<p><span class="versionmodified added">3.2 新版功能: </span><em>annotate</em> 参数。</p>
</div>
</dd></dl>

<dl class="function">
<dt id="pickletools.genops">
<code class="sig-prename descclassname">pickletools.</code><code class="sig-name descname">genops</code><span class="sig-paren">(</span><em class="sig-param">pickle</em><span class="sig-paren">)</span><a class="headerlink" href="#pickletools.genops" title="永久链接至目标">¶</a></dt>
<dd><p>提供包含 pickle 中所有操作码的 <a class="reference internal" href="../glossary.html#term-iterator"><span class="xref std std-term">iterator</span></a>，返回一个 <code class="docutils literal notranslate"><span class="pre">(opcode,</span> <span class="pre">arg,</span> <span class="pre">pos)</span></code> 三元组的序列。 <em>opcode</em> 是 <code class="xref py py-class docutils literal notranslate"><span class="pre">OpcodeInfo</span></code> 类的一个实例；<em>arg</em> 是 Python 对象形式的 opcode 参数的已解码值；<em>pos</em> 是 opcode 所在的位置。 <em>pickle</em> 可以是一个字符串或一个文件类对象。</p>
</dd></dl>

<dl class="function">
<dt id="pickletools.optimize">
<code class="sig-prename descclassname">pickletools.</code><code class="sig-name descname">optimize</code><span class="sig-paren">(</span><em class="sig-param">picklestring</em><span class="sig-paren">)</span><a class="headerlink" href="#pickletools.optimize" title="永久链接至目标">¶</a></dt>
<dd><p>在消除未使用的 <code class="docutils literal notranslate"><span class="pre">PUT</span></code> 操作码之后返回一个新的等效 pickle 字符串。 优化后的 pickle 将更为简短，耗费更为的传输时间，要求更少的存储空间并能更高效地解封。</p>
</dd></dl>

</div>
</div>


          </div>
        </div>
      </div>
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
  <h3><a href="../contents.html">目录</a></h3>
  <ul>
<li><a class="reference internal" href="#"><code class="xref py py-mod docutils literal notranslate"><span class="pre">pickletools</span></code> --- pickle 开发者工具集</a><ul>
<li><a class="reference internal" href="#command-line-usage">命令行语法</a><ul>
<li><a class="reference internal" href="#command-line-options">命令行选项</a></li>
</ul>
</li>
<li><a class="reference internal" href="#programmatic-interface">编程接口</a></li>
</ul>
</li>
</ul>

  <h4>上一个主题</h4>
  <p class="topless"><a href="dis.html"
                        title="上一章"><code class="xref py py-mod docutils literal notranslate"><span class="pre">dis</span></code> --- Python 字节码反汇编器</a></p>
  <h4>下一个主题</h4>
  <p class="topless"><a href="misc.html"
                        title="下一章">杂项服务</a></p>
  <div role="note" aria-label="source link">
    <h3>本页</h3>
    <ul class="this-page-menu">
      <li><a href="../bugs.html">提交 Bug</a></li>
      <li>
        <a href="https://github.com/python/cpython/blob/3.7/Doc/library/pickletools.rst"
            rel="nofollow">显示源代码
        </a>
      </li>
    </ul>
  </div>
        </div>
      </div>
      <div class="clearer"></div>
    </div>  
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>导航</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="总目录"
             >索引</a></li>
        <li class="right" >
          <a href="../py-modindex.html" title="Python 模块索引"
             >模块</a> |</li>
        <li class="right" >
          <a href="misc.html" title="杂项服务"
             >下一页</a> |</li>
        <li class="right" >
          <a href="dis.html" title="dis --- Python 字节码反汇编器"
             >上一页</a> |</li>
        <li><img src="../_static/py.png" alt=""
                 style="vertical-align: middle; margin-top: -1px"/></li>
        <li><a href="https://www.python.org/">Python</a> &#187;</li>
        <li>
          <a href="../index.html">3.7.8 Documentation</a> &#187;
        </li>

          <li class="nav-item nav-item-1"><a href="index.html" >Python 标准库</a> &#187;</li>
          <li class="nav-item nav-item-2"><a href="language.html" >Python 语言服务</a> &#187;</li>
    <li class="right">
        

    <div class="inline-search" style="display: none" role="search">
        <form class="inline-search" action="../search.html" method="get">
          <input placeholder="快速搜索" type="text" name="q" />
          <input type="submit" value="转向" />
          <input type="hidden" name="check_keywords" value="yes" />
          <input type="hidden" name="area" value="default" />
        </form>
    </div>
    <script type="text/javascript">$('.inline-search').show(0);</script>
         |
    </li>

      </ul>
    </div>  
    <div class="footer">
    &copy; <a href="../copyright.html">版权所有</a> 2001-2020, Python Software Foundation.
    <br />
    Python 软件基金会是一个非盈利组织。
    <a href="https://www.python.org/psf/donations/">请捐助。</a>
    <br />
    最后更新于 6月 29, 2020.
    <a href="../bugs.html">发现了问题</a>？
    <br />
    使用<a href="http://sphinx.pocoo.org/">Sphinx</a>2.3.1 创建。
    </div>

  </body>
</html>